﻿using Newtonsoft.Json;
using PlayListSet.Comm;
using PlayListSet.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Media3D;

namespace PlayListSet.Entity
{
    public class PlayCmd:PlayInfoBase
    {
        public PlayCmd()
        {
            UIUserCommand = new UserCommand();
            UIUserCommand.ExecuteEvent += UserCommand_ExecuteEvent;
        }

     
        #region UI Model

        [JsonIgnore]
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public UserCommand UIUserCommand { set; get; }
        private void UserCommand_ExecuteEvent(object parameter)
        {
            string p = parameter as string;
            if (p == "test")
            {
                Task.Factory.StartNew(() => {
                    try
                    {
                        Comm.PlayCMDControl.RunNet(this);
                        AppControl.Instance.Log($"播控网络指令{Name}执行成功");
                        MessageBox.Show($"播控网络指令{Name}执行成功");
                    }
                    catch (Exception ex)
                    {
                        AppControl.Instance.Log($"播控网络指令{Name}执行失败,{ex.Message}");
                        MessageBox.Show($"播控网络指令{Name}执行失败,{ex.Message}");

                    }
                });
            }
        }
        #endregion

        [SqlSugar.SugarColumn(IsIgnore = true)]
        [JsonIgnore]
        public string ChannelInfoStr
        {
            get { return Channel; }
        }


        private int cmdType;
        /// <summary>
        /// 0 stop 1 pause 2 seek   3 net
        /// </summary>
        public int CMDType
        {
            set { cmdType = value; PropertyChangedEvent("CMDType"); }
            get { return cmdType; }
        }
        private double seekTime;
        public double SeekTime
        {
            set { seekTime = value; PropertyChangedEvent("SeekTime"); }
            get { return seekTime; }
        }
        private int netType;
        /// <summary>
        /// 0 UDP 1 tcp
        /// </summary>
        public int NetType
        {
            set { netType = value; PropertyChangedEvent("NetType"); }
            get { return netType; }
        }
        private int dataType;
        /// <summary>
        /// 0 asic 1 utf 2 16hex
        /// </summary>
        public int DataType
        {
            set { dataType = value; PropertyChangedEvent("DataType"); }
            get { return dataType; }
        }
        private string data=string.Empty;

    
        public string Data
        {
            set { data = value; PropertyChangedEvent("Data"); }
            get { return data; }
        }
        private string netInfo=string.Empty;
        public string NetInfo
        {
            set { netInfo = value; PropertyChangedEvent("NetInfo"); }
            get { return netInfo; }
        }

        public static void Save(List<PlayCmd> infos)
        {
            //var json = Newtonsoft.Json.JsonConvert.SerializeObject(infos, Formatting.Indented);
            //System.IO.File.WriteAllText(
            //    System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "cfg", "PlayCmds.json"),
            //    json, Encoding.UTF8);
            var db = AppHelp.GetAppSqlSugarClient();
            db.Deleteable<PlayCmd>().ExecuteCommand();
            db.Insertable<PlayCmd>(infos).ExecuteCommand();

        }
        public static List<PlayCmd> Read()
        {
            //var text = System.IO.File.ReadAllText(
            //     System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "cfg", "PlayCmds.json"), Encoding.UTF8);
            //return Newtonsoft.Json.JsonConvert.DeserializeObject<List<PlayCmd>>(text);
            var cfg = AppHelp.GetAppSqlSugarClient().Queryable<PlayCmd>().ToList();
            if (cfg == null)
                return new List<PlayCmd>();
            return cfg;
        }
      
    }
}
